﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using MovieBrowser.Controller;
using MovieBrowser.Event;
using MovieBrowser.Model;

namespace MovieBrowser.Util
{
    public class CollectInformation
    {
        public IList<Person> Persons;
        public IList<Movie> ListNodes { private get; set; }
        public MovieBrowserController MovieController { private get; set; }
        public bool FetchKeyword { get; set; }
        public bool UpdateNode { get; set; }

        public event EventHandler OnDebugText;
        public void InvokeOnDebugText(Exception exception)
        {
            var handler = OnDebugText;
            if (handler != null) handler(this, new DebugTextEventArgs(exception));
        }
        public void InvokeOnDebugText(string text)
        {
            var handler = OnDebugText;
            if (handler != null) handler(this, new DebugTextEventArgs(text));
        }

        public void CollectPersonInformation()
        {
            try
            {
                if (Persons == null)
                {
                    throw new Exception("Persons: Empty person list.");
                }

                for (var index = 0; index < Persons.Count; index++)
                {
                    var person = Persons[index];
                    MovieController.CollectAndAddPersonToDb(person);
                    InvokeOnDebugText(string.Format("Processed: {0}/{1} -- {2}", index + 1, Persons.Count, Persons[index].Name));
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(@"Problem Collecting Information.", exception.Message);
            }
        }

        public void CollectFullInformation()
        {
            try
            {
                if (ListNodes == null)
                {
                    throw new Exception("ListNodes: Empty movies list.");
                }

                for (var index = 0; index < ListNodes.Count; index++)
                {
                    InvokeOnDebugText(string.Format("Processing: {0}/{1} -- {2}", index + 1, ListNodes.Count, ListNodes[index].Title));
                    var rowMovie = ListNodes[index];
                    var movie = MovieController.CollectAndAddMovieToDb(rowMovie, null, FetchKeyword);

                    if (movie != null && UpdateNode)
                    {
                        MovieController.UpdateMovieNode(rowMovie, movie);
                    }
                    InvokeOnDebugText(string.Format("Finished: {0}/{1} -- {2}", index + 1, ListNodes.Count, ListNodes[index].Title));
                }
                //MessageBox.Show(@"Information Collection Finished.");
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message + "\n" + exception.InnerException.Message + "\n" + exception.StackTrace, @"Problem Collecting Information.");

            }
        }

        public void CollectKeywords()
        {
            try
            {
                if (ListNodes == null)
                {
                    throw new Exception("ListNodes: Empty movies list.");
                }

                for (var index = 0; index < ListNodes.Count; index++)
                {
                    InvokeOnDebugText(string.Format("Processing: {0}/{1} -- {2}", index + 1, ListNodes.Count, ListNodes[index].Title));

                    var rowMovie = ListNodes[index];
                    MovieController.CollectKeywords(rowMovie);
                }
                //MessageBox.Show(@"Keywords Collection Finished.");
            }
            catch (Exception exception)
            {
                MessageBox.Show(@"Problem Collecting Keywords.", exception.Message);
            }
        }

    }
}